package com.h2cloud.aspect.aopInvokeProcess;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author: wulongkai
 * @create: 2022-10-11 15:45
 **/
//@Aspect
@Component
@Slf4j
@Order(2)
public class aopAdviceOrderV2 {

    @Pointcut("execution(* com.h2cloud.aspect.WorkFlowBaseService.testAdviceOrder(..))")
    public void pointcut() {
    }

    @Before("pointcut()")
    public void Before() {
        log.info("========================================== BeforeV2 Start ==========================================");
        log.info("BeforeV2");
        log.info("========================================== BeforeV2 end ==========================================");
    }

    @After("pointcut()")
    public void After() {
        log.info("========================================== AfterV2 Start ==========================================");
        log.info("After");
        log.info("========================================== AfterV2 end ==========================================");
    }

    @AfterReturning(pointcut = "pointcut()", returning = "returnObject")
    public void afterReturning(JoinPoint joinPoint, Object returnObject) {
        log.info("========================================== afterReturningV2 Start ==========================================");
        log.info("afterReturningV2: {}", returnObject);
        log.info("========================================== afterReturningV2 end ==========================================");
    }

    @AfterThrowing(pointcut = "pointcut()", throwing = "ex")
    public void afterThrowing(Throwable ex) {
        log.info("========================================== afterThrowingV2 Start ==========================================");
        log.info("afterThrowingV2", ex);
        log.info("========================================== afterThrowingV2 end ==========================================");
    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("========================================== aroundV2 Start ==========================================");
        try {
            log.info("aroundV2");
            return joinPoint.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
            throw e;
        } finally {
            log.info("========================================== aroundV2 end ==========================================");
        }
    }
}
